# -*- coding: utf-8 -*-
# @Time    : 2022/1/12 9:11
# @Author  : 魏福齐
# @Email   : 775776913@qq.com
# @File    : page_finance_payables.py
# @Software: PyCharm
from pages.page_home import HomePage
from pages.page_login import LoginPage
from pages.base_page import BasePage
from pages.page_finance_receivables import PageFinanceReceivables
from selenium.webdriver.common.by import By

class PageFinancePayables(BasePage):
    '''应付款页面'''
    jump_yfk_locator = (By.CSS_SELECTOR, 'body > div.container > div.page-header > ul > li:nth-child(2) > a')
    #跳转到应付款页面
    add_yfk_submit_locator = (By.CSS_SELECTOR, 'body > div.container > div.row > div:nth-child(1) > '
                                               'div.pull-right > a:nth-child(1)')
    #【添加应付款】按钮
    yfk_name_input_locator = (By.CSS_SELECTOR, '#name')
    #应付款名输入框
    yfk_lst_name_table_locator = (By.CSS_SELECTOR, '#form1 > table > tbody')
    #应付款列表tbody
    hetong_input_locator = (By.CSS_SELECTOR, '#contract_name')
    #合同选择框
    hetong_tbody_locator = (By.CSS_SELECTOR, '#data')
    #合同table
    bjhetong_table_ok_locator = (By.CSS_SELECTOR,
                                 'body > div:nth-child(10) > div.ui-dialog-buttonpane.ui-widget-content.ui-helper'
                                 '-clearfix > div > button:nth-child(1)')
    #编辑合同后的【ok】按钮

    fuzeren_input_locator = (By.CSS_SELECTOR, '#owner_name')
    #负责人输入框
    fuzeren_tbody_locator = (By.CSS_SELECTOR, '#d_content')
    #负责人table
    bjfuzeren_table_ok_locator = (By.CSS_SELECTOR,
                                  'body > div:nth-child(9) > div.ui-dialog-buttonpane.ui-widget-content.ui-helper'
                                  '-clearfix > div > button:nth-child(1)')
    #负责人table的【ok】按钮
    yfk_chenggong_text=(By.XPATH,'/html/body/div[5]/div[2]')
    del_success_text=(By.CSS_SELECTOR,'body > div.container > div.alert.alert-success')


    def jump_yfk(self):
        '''点击应付款，进入应付款列表'''
        self.driver.find_element(*self.jump_yfk_locator).click()

    def add_yfk(self):
        '''点击【添加应付款】'''
        self.driver.find_element(*self.add_yfk_submit_locator).click()

    def yfk_name_input(self,yfkname):
        '''输入应付款名'''
        self.driver.find_element(*self.yfk_name_input_locator).send_keys(yfkname)

    def yfk_lst_update(self,yfkname):
        '''找到已存在的应付款，点击【修改】'''
        table = self.driver.find_element(*self.yfk_lst_name_table_locator)
        # 获取应收款列表
        trs = table.find_elements(By.TAG_NAME, 'tr')
        for tr in trs:
            tds = tr.find_elements(By.TAG_NAME, 'td')
            if tds[1].text == yfkname:
                tds[-1].find_element(By.LINK_TEXT, '修改').click()
                break

    def hetong_update(self,khname):
        '''重新选择合同'''
        self.driver.find_element(*self.hetong_input_locator).click()
        # 点击合同选择框
        table = self.driver.find_element(*self.hetong_tbody_locator)
        # 获取合同table
        trs = table.find_elements(By.TAG_NAME, 'tr')
        for i in trs:
            tds = i.find_elements(By.TAG_NAME, 'td')
            if tds[3].text == khname:
                tds[0].find_element(By.TAG_NAME, 'input').click()
                # 选中合同
        self.driver.find_element(*self.bjhetong_table_ok_locator).click()

    def fuzeren_update(self,fzr):
        '''重新选择负责人'''
        self.driver.find_element(*self.fuzeren_input_locator).click()
        # 点击负责人选择框
        table = self.driver.find_element(*self.fuzeren_tbody_locator)
        trs = table.find_elements(By.TAG_NAME, 'tr')
        for i in trs:
            tds = i.find_elements(By.TAG_NAME, 'td')
            if tds[1].text == fzr:
                tds[0].find_element(By.TAG_NAME, 'input').click()
        # 选中负责人
        self.driver.find_element(*self.bjfuzeren_table_ok_locator).click()

    def assert_edit_payables(self):
        '''获取编辑成功文本'''
        return self.driver.find_element(*self.yfk_chenggong_text).text



    def add_payables(self,payablename,costomor,pic,price,time,describe):
        '''添加应付款'''
        cw = PageFinanceReceivables(self.driver)
        cw.jump_finance()
        self.jump_yfk()
        self.add_yfk()
        self.yfk_name_input(payablename)
        cw.ysk_hetong_check(costomor)
        cw.ysk_fuzeren_check(pic)  # 选择负责人
        cw.ysk_price_input(price)  # 输入应付款金额
        cw.ysk_time_input(time)  # 输入付款时间
        cw.ysk_miaoshu(describe)  # 输入描述
        cw.save()

    def assert_add_payables(self):
        cw = PageFinanceReceivables(self.driver)
        return cw.assert_add_receivable()

    def edit_payables(self,payablename,rename,costomor,price,pic):
        '''编辑应付款'''
        cw = PageFinanceReceivables(self.driver)
        cw.jump_finance()
        self.jump_yfk()
        self.yfk_lst_update(payablename)
        cw.ysk_update_name(rename)  # 修改应付款名
        self.hetong_update(costomor)  # 选择合同
        cw.ysk_update_price(price)  # 修改应付款金额
        self.fuzeren_update(pic)  # 选择负责人
        cw.save()




    def del_payables(self,payablename):
        '''删除应付款'''
        cw = PageFinanceReceivables(self.driver)
        cw.jump_finance()
        self.jump_yfk()
        cw.ysk_lst_del(payablename)  # 勾选要删除的应收款
        cw.shanchu_submit()  # 点击【删除】按钮
        cw.shanchu_accept()  # 点击警告框的确定
        cw.ysk_huishouzhan_submit()  # 点击【回收站】
        cw.huishouzhan_gouxuan()  # 勾选回收站的应收款
        cw.shanchu_submit()  # 点击【删除】按钮
        cw.shanchu_accept()  # 点击警告框的确定

    def assert_del_payables(self):
        '''删除成功文本'''
        return self.driver.find_element(*self.del_success_text).text






